*Put directory path here, and put data here. Create a subfolder called "Output".
cd "Drive:\Folder"
*Set matrix sizes
set matsize 11000
set maxvar 32767
*Set scheme
set scheme s1mono




*Table 1: Descriptive Statistics of Individuals in Sample
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
gen GPvisit_age15_cond=GPvisit_age15 if GPvisit_age15>0
*Globals of variables
gl char "birthorder siblings NORborn mom_age_birth mom_married  " 
gl char "$char mom_edu_yrs_15 mom_log_tot_inc_15 mom_NoLFP_15 " 
gl GP "GPvisit_age15 GPvisit_age15_cond str_keepexogGP str_GPsexMatch "
gl outc "GPA SciGPA GPA_HS SciGPA_HS Acad_track_Yr1 STEM_HSdiploma"
gl outc "$outc CollEver STEMCollEver "
eststo clear
estpost summarize $outc $GP $health if sex==2 & exposure_str_exog_swap!=. & STEM_HSdiploma!=.
esttab using "Output\Table_1_Female.csv", cells("mean sd") replace
eststo clear
estpost summarize $char $GP $outc if sex==1 & exposure_str_exog_swap!=. & STEM_HSdiploma!=.
esttab using "Output\Table_1_Male.csv", cells("mean sd") replace
}



*Table 2: Effect of same-gender GP on educational choice and performance
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
*Globals of variables
gl out_core "Acad_track_Yr1 STEM_HSdiploma SciGPA SciGPA_HS "
*Panel A: Girls
eststo clear
foreach out in $out_core{
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if sex==2 & exposure_str_exog_swap!=., cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_2_Female.csv", replace ///
	keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	note("Previous GP FE, municipality FE, cohort FE, and year FE")
*Panel B: Boys
eststo clear
foreach out in $out_core{
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if sex==1 & exposure_str_exog_swap!=., cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_2_Male.csv", replace ///
	keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) ///
	note("Previous GP FE, municipality FE, cohort FE, and year FE")
}



*Table 3: Effect of same-gender GP on females, by mother’s education
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
*Globals of variables
gl out_core "Acad_track_Yr1 STEM_HSdiploma SciGPA SciGPA_HS "
*Panel A: Mother college or more
eststo clear
foreach out in $out_core{
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if (mom_edu_yrs>=16 & mom_edu_yrs<21.5) & sex==2 & exposure_str_exog_swap!=., ///
cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_3_Female_MomCollOrMore.csv", replace ///
	keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	note("Previous GP FE, municipality FE, cohort FE, and year FE")
*Panel B: Mother less than college
eststo clear
foreach out in $out_core{
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if (mom_edu_yrs<16) & sex==2 & exposure_str_exog_swap!=., ///
cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_3_Female_MomLessColl.csv", replace ///
	keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	note("Previous GP FE, municipality FE, cohort FE, and year FE")	
}



*Table 4: The effect of same-gender GP on STEMM GPA - quantile effects
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
*Panel A: Compulsory school STEMM GPA
eststo clear
forvalues q = 0.1(0.1)0.9 {
xi: rifreg SciGPA str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
 i.str_exogGPIDprev if sex==2 & exposure_str_exog_swap!=., quantile(`q')
eststo
}
esttab using "Output\Table_4_Female_QuantileLowerSciGPA.csv", replace ///
	keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	note("Previous GP FE, municipality FE, cohort FE, and year FE. Bootstrap SE")
*Panel B: High school STEMM GPA
eststo clear
forvalues q = 0.1(0.1)0.9 {
xi: rifreg SciGPA_HS str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
i.str_exogGPIDprev if sex==2 & exposure_str_exog_swap!=., quantile(`q')
eststo
}
esttab using "Output\Table_4_Female_QuantileUpperSciGPA.csv", replace ///
	keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	note("Previous GP FE, municipality FE, cohort FE, and year FE. Bootstrap SE")
}



*Table 5: Effect of same-gender GP, additional outcomes
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
*Globals of variables
gl outc "NonSciGPA NonSciGPA_HS CollEver STEMCollEver MedCollEver STEMCollEver_NotMed "
*Analysis
eststo clear
foreach out in $outc{
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if sex==2 & exposure_str_exog_swap!=., cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_5_Female_AdditionalOutcomes.csv", replace ///
	keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	note("Previous GP FE, municipality FE, cohort FE, and year FE")
}



*Table 6: Balance test
{
*Load and merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
*Restrict sample to be consistent w/main results
drop if STEM_HSdiploma==.
*Declare globals
gl char "birthorder siblings NORborn " 
gl char "$char mom_edu_yrs_15 mom_age_birth mom_married " 
gl char "$char mom_log_tot_inc_15 mom_NoLFP_15 ""
*Analysis
eststo clear
foreach out in $char{
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if sex==2 & exposure_str_exog_swap!=., cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_6_Female_BalanceTeset.csv", replace ///
	keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	addnote("Previous GP FE; municipality FE; cohort FE; and year FE." ///
	"Sample limited to those with education data.")
}



*Table 7: Placebo Tests; Post high school GP swaps
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
*Globals of variables
gl out_core "Acad_track_Yr1 STEM_HSdiploma SciGPA SciGPA_HS "
*Panel A: All girls who were subject to their first exogenous GP swap between age 20 and 25
eststo clear
foreach out in $out_core{
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if sex==2 & (str_exog_age>=20 & str_exog_age<=25), ///
cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_7_Female_Swaps20-25.csv", replace ///
	keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	note("Previous GP FE, municipality FE, cohort FE, and year FE")
*Panel B: All girls who were subject to their first exogenous GP swap between age 17 and 20
eststo clear
foreach out in Acad_track_Yr1  SciGPA {
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if sex==2 & (str_exog_age>=17 & str_exog_age<=20) & SciGPA_HS!=., ///
cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_7_Female_Swaps17-20.csv", replace ///
	keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	note("Previous GP FE, municipality FE, cohort FE, and year FE")
}



*Table 8: P-values of permutation test
{
clear all
*Load data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
bysort str_exogGPID: gen first_ob = _n
replace first_ob = . if first_ob!=1
*Set percent of match
sum str_GPsexMatch if sex==2 & exposure_str_exog_swap!=.
global percentmatch = r(mean)
*Get main estimates and put in globals
eststo clear
areg Acad_track_Yr1 str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
	if sex==2 & exposure_str_exog_swap!=., cl(str_exogGPID) absorb(str_exogGPIDprev)
global Acad_track_Yr1main = _b[str_GPsexMatch] 
	disp $Acad_track_Yr1main
areg STEM_HSdiploma str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
	if sex==2 & exposure_str_exog_swap!=., cl(str_exogGPID) absorb(str_exogGPIDprev)
global STEM_HSdiplomamain = _b[str_GPsexMatch] 
	disp $STEM_HSdiplomamain
areg SciGPA str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
	if sex==2 & exposure_str_exog_swap!=., cl(str_exogGPID) absorb(str_exogGPIDprev)
global SciGPAmain = _b[str_GPsexMatch] 
	disp $SciGPAmain
areg SciGPA_HS str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
	if sex==2 & exposure_str_exog_swap!=., cl(str_exogGPID) absorb(str_exogGPIDprev)
global SciGPAmain = _b[str_GPsexMatch] 
	disp $SciGPA_HSmain
*Permutation test based p-values
eststo clear
*Loop through main outcomes
foreach dep in STEM_HSdiploma SciGPA Acad_track_Yr1 SciGPA_HS{
*Loop through 300 iterations to get distribution of synthetic estimates	
forvalues i=1(1)300 {
	replace str_GPsexMatch=.
	replace str_GPsexMatch = runiform() if first_ob==1
	egen tempstr_GPsexMatch = max(str_GPsexMatch), by(str_exogGPID)
	
	replace str_GPsexMatch = 0 if tempstr_GPsexMatch <= $percentmatch
	replace str_GPsexMatch = 1 if tempstr_GPsexMatch > $percentmatch
	drop tempstr_GPsexMatch
	
	eststo: areg `dep' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
	if sex==2 & exposure_str_exog_swap!=., cl(str_exogGPID) absorb(str_exogGPIDprev)
}
*Put synthetic estimates in CSV files
esttab using "Table_8 PermTestDistribution_`dep'.csv", ///
nostar keep(str_GPsexMatch) replace
eststo clear
}
}

		

*Table 9: Potential pathways and mechanisms
{
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
merge 1:1 lopenr using "HealthOutcomes.dta"
drop _merge
merge 1:1 lopenr using "MomHealthOutcomes.dta"
*Globals of variables
gl health "GPvisit_age15 str_keepexogGP DxCt15 DxMentalHealth15 "
gl health "$health ReproBinary15 Fertility15 "
gl mother "mom_edu_yrs_15 mom_log_tot_inc_15 mom_NoLFP_15 "
gl mother "$mother MomDxCt15 MomDxMentalHealth15 MomReproBinary15 MomFertility15 "

*Panel A: Potential indirect effects through health
eststo clear
foreach out in $health{
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if sex==2 & exposure_str_exog_swap!=. & STEM_HSdiploma!=., ///
cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_9_Female_Health.csv", replace ///
	keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	note("Previous GP FE, municipality FE, cohort FE, and year FE")
*Panel B: Potential indirect effects through mother
eststo clear
foreach out in $mother{
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if sex==2 & exposure_str_exog_swap!=. & STEM_HSdiploma!=., ///
cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_9_Female_Mother.csv", replace ///
	keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) ///
	note("Previous GP FE, municipality FE, cohort FE, and year FE")
}



*Figure 1: Same-gender role model effects by age-at-swap, females		
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
*Globals of variables
gl out_core "Acad_track_Yr1 STEM_HSdiploma SciGPA SciGPA_HS "
*Age varying treatment effects
gen str_exog_AgeSwapBins1 = str_GPsexMatch if str_exog_age>=6 & str_exog_age<=9
replace str_exog_AgeSwapBins1 = 0 if !(str_exog_age>=6 & str_exog_age<=9) & str_GPsexMatch!=.
gen str_exog_AgeSwapBins2 =  str_GPsexMatch if str_exog_age>=10 & str_exog_age<=12 
replace str_exog_AgeSwapBins2 = 0 if !(str_exog_age>=10 & str_exog_age<=12) & str_GPsexMatch!=.
gen str_exog_AgeSwapBins3 =  str_GPsexMatch if str_exog_age>=13 & str_exog_age!=.
replace str_exog_AgeSwapBins3 = 0 if !(str_exog_age>=13 & str_exog_age!=.) & str_GPsexMatch!=.
label var str_exog_AgeSwapBins1 "6-9"
label var str_exog_AgeSwapBins2 "10-12"
label var str_exog_AgeSwapBins3 "13+"
*Analysis
eststo clear
foreach out in $outc{
eststo: areg `out' ///
str_exog_AgeSwapBins1 str_exog_AgeSwapBins2 str_exog_AgeSwapBins3 ///
i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if sex==2 & exposure_str_exog_swap!=., cl(str_exogGPID) absorb(str_exogGPIDprev)
}
esttab using "Output\Figure_1_Female_AgeOfSwap.txt", replace ///
	keep(str_exog_AgeSwapBins1 str_exog_AgeSwapBins2 str_exog_AgeSwapBins3) ///
	label nonumber b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	note("Previous GP FE, municipality FE, cohort FE, and year FE")
}			



*Figure 2: Estimated effects with varying fixed effects	
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
drop _merge
merge m:1 str_exogGPIDprev using "PrevGP_Chars.dta"
drop _merge
gen YrByMuni= str_exog_muni * 10000 + yr_str_exog_swap
gen CohByMuni = str_exog_muni * 10000 + Byr
*Globals of variables
gl out_core "Acad_track_Yr1 STEM_HSdiploma SciGPA SciGPA_HS "
*Deal with missing prev GP char
gen str_exogGPsexprev_miss=1 if str_exogGPsexprev==.
replace str_exogGPsexprev_miss=0 if str_exogGPsexprev!=.
replace str_exogGPsexprev=0 if str_exogGPsexprev==.
gen YearBorn_prevGP_miss=1 if YearBorn_prevGP==.
replace YearBorn_prevGP_miss=0 if YearBorn_prevGP!=.
replace YearBorn_prevGP=0 if YearBorn_prevGP==.
gen Norway_prevGP_miss=1 if Norway_prevGP==.
replace Norway_prevGP_miss=0 if Norway_prevGP!=.
replace Norway_prevGP=0 if Norway_prevGP==.
*Analysis
eststo clear
foreach out in $out_core{					
eststo: reg `out' str_GPsexMatch   ///											No FE
	if sex==2 & exposure_str_exog_swap!=. & str_exog_muni!=. ///
	& yr_str_exog_swap!=. & Byr!=., cl(str_exogGPID) 
eststo: areg `out' str_GPsexMatch i.CohByMuni  ///								Muni-by-cohort FE
	if sex==2 & exposure_str_exog_swap!=. & str_exog_muni!=. ///
	& yr_str_exog_swap!=. & Byr!=., cl(str_exogGPID)  absorb(CohByMuni)
eststo: reg `out' str_GPsexMatch i.str_exog_muni i.Byr i.str_exog_muni#c.Byr ///Cohort FE, muni trends
	if sex==2 & exposure_str_exog_swap!=. & str_exog_muni!=. ///
	& yr_str_exog_swap!=. & Byr!=., cl(str_exogGPID) 
eststo: reg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///	Year, Cohort, Muni FE
	if sex==2 & exposure_str_exog_swap!=., cl(str_exogGPID) 
eststo: reg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///	Year, Cohort, Muni FE; pref FE with char
	str_exogGPsexprev YearBorn_prevGP Norway_prevGP ///
	str_exogGPsexprev_miss YearBorn_prevGP_miss Norway_prevGP_miss ///
	if sex==2 & exposure_str_exog_swap!=., cl(str_exogGPID) 
eststo: areg `out' str_GPsexMatch i.Byr  ///									Prev GP FE, and cohort
	if sex==2 & exposure_str_exog_swap!=., ///
	cl(str_exogGPID) absorb(str_exogGPIDprev)
eststo: areg `out' str_GPsexMatch i.Byr i.str_exog_muni ///						Prev GP FE, Muni and cohort
	if sex==2 & exposure_str_exog_swap!=., ///
	cl(str_exogGPID) absorb(str_exogGPIDprev)
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr  ///					Prev GP FE, Year and cohort FE
	if sex==2 & exposure_str_exog_swap!=., ///
	cl(str_exogGPID) absorb(str_exogGPIDprev)
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///	Prev GP FE, Year, Cohort, and Muni FE
	if sex==2 & exposure_str_exog_swap!=., cl(str_exogGPID) absorb(str_exogGPIDprev)
esttab using "Output\Figure_2_Female_VaryingFE_`out'.csv", replace ///
	keep(str_GPsexMatch) ///
	label  stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	title("Main analysis with varying FEs") ///
	addnote( ///
	"Model 1: No fixed effects" ///
	"Model 2: Cohort-by-municipality fixed effects" ///
	"Model 3: Cohort fixed effects and municipality-specific trends" ///
	"Model 4: Cohort, municipality, and year fixed effects" ///
	"Model 5: Cohort, municipality, and year fixed effects, and controls for previous GP chars" ///
	"Model 6: Cohort and previous GP fixed effects" ///
	"Model 7: Cohort, municipality, and previous GP fixed effects" ///
	"Model 8: Cohort, year, and previous GP fixed effects" ///
	"Model 9: Cohort, municipality, year, and previous GP fixed effects" ///
	)
}	
}	



*Table A1: Overview of high school educational programs and specializations			
*See citation for relevant information



*Table A2: Descriptive statistics of individuals not in sample
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
gen GPvisit_age15_cond=GPvisit_age15 if GPvisit_age15>0
*Globals of variables
gl char "birthorder siblings NORborn mom_age_birth mom_married  " 
gl char "$char mom_edu_yrs_15 mom_log_tot_inc_15 mom_NoLFP_15 " 
gl GP "GPvisit_age15 GPvisit_age15_cond str_keepexogGP str_GPsexMatch "
gl outc "GPA SciGPA GPA_HS SciGPA_HS Acad_track_Yr1 STEM_HSdiploma"
gl outc "$outc CollEver STEMCollEver "
eststo clear
estpost summarize $outc $GP $health if sex==2 & exposure_str_exog_swap==. & STEM_HSdiploma!=.
esttab using "Output\Table_A2_Female.csv", cells("mean sd") replace
eststo clear
estpost summarize $char $GP $outc if sex==1 & exposure_str_exog_swap==. & STEM_HSdiploma!=.
esttab using "Output\Table_A2_Male.csv", cells("mean sd") replace
}



*Table A3: Swap frequency
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
*Determine swap frequency
tabm total_str_exog_swap_age15 if exposure_str_exog_swap!=., ///
	matcell(freq) matrow(names) transpose
*Put results in Excel
putexcel set "Output\Table_A3_SwapFreq", replace
putexcel A1=("Frequency of main swap types; conditional str exog swap before 15") ///
	A2=("Freq") B2=("Exog swap") 
putexcel set "Output\Table_A3_SwapFreq", modify
putexcel A3=matrix(names) B3=matrix(freq)
}		



*Table A4: Probability of staying with GP as a function of observable characteristics
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
*Restrict sample to be consistent w/main results
drop if STEM_HSdiploma==.
*Declare globals
gl char "birthorder siblings NORborn " 
gl char "$char mom_edu_yrs_15 mom_age_birth mom_married " 
gl char "$char mom_log_tot_inc_15 mom_NoLFP_15 ""
*Analysis
eststo clear
foreach out in $char{
eststo: areg `out' str_keepexogGP i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if sex==2 & exposure_str_exog_swap!=., cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_A4_StayGPBalance.csv", replace ///
	keep(str_keepexogGP) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	title("Balance test; keep GP vs leave GP; female; cluster new GP") ///
	addnote("Previous GP FE; municipality FE; cohort FE; and year FE." ///
	"Sample limited to those with education data.")
}			



*Table A5: Effect of same-gender GP on females, additional outcomes			
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
*Globals of variables
gl out "NonSTEM_Acad_track_Yr3 NonAcad_track_Yr3 "
*Analysis
eststo clear
foreach out in $out{
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if sex==2 & exposure_str_exog_swap!=., cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_A5_AdditionalOutcomes.csv", replace ///
	keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	note("Previous GP FE, municipality FE, cohort FE, and year FE")
}			



*Table A6: Effect of same-gender GP on female ..., stayers v. leavers			
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
*Globals of variables
gl out_core "Acad_track_Yr1 STEM_HSdiploma SciGPA SciGPA_HS "
*Panel A: Individuals remaining with their exogenously assigned GP until age 15
eststo clear
foreach out in $out_core{
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if str_keepexogGP==1 & sex==2 & exposure_str_exog_swap!=., ///
cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_A6_Stayers.csv", replace keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	note("Previous GP FE, municipality FE, cohort FE, and year FE")	
*Panel B: Individuals not remaining with their exogenously assigned GP until age 15
eststo clear
foreach out in $out_core{
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if str_keepexogGP==0 & sex==2 & exposure_str_exog_swap!=., ///
cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_A6_Leavers.csv", replace keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	note("Previous GP FE, municipality FE, cohort FE, and year FE")	
}			
	
	
	
*Table A7: Effect of same-gender GP on female ..., GP list termination only
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
*Globals of variables
gl out_core "Acad_track_Yr1 STEM_HSdiploma SciGPA SciGPA_HS "
*Analysis
eststo clear
foreach out in $out_core{
eststo: areg `out' term_GPsexMatch i.yr_term_exog_swap i.Byr i.term_exog_muni ///
if sex==2 & exposure_term_exog_swap!=., cl(term_exogGPID) absorb(term_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_A7_TerminationOnly.csv", replace keep(term_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	note("Previous GP FE, municipality FE, cohort FE, and year FE")
}		



*Table A8: Effect of same-gender GP on male ..., low-educated mothers		
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
*Globals of variables
gl out_core "Acad_track_Yr1 STEM_HSdiploma SciGPA SciGPA_HS "
*Analysis
eststo clear
foreach out in $out_core{
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if (mom_edu_yrs<16) & sex==1 & exposure_str_exog_swap!=., ///
cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_A8_Male_MomLessColl.csv", replace ///
	keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	note("Previous GP FE, municipality FE, cohort FE, and year FE")	
}			



*Table A9: Effect of same-gender GP on female ..., age-at-swap		
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
*Globals of variables
gl out_core "Acad_track_Yr1 STEM_HSdiploma SciGPA SciGPA_HS "
*Age varying treatment effects
gen str_exog_AgeSwapBins1 = str_GPsexMatch if str_exog_age>=6 & str_exog_age<=9
replace str_exog_AgeSwapBins1 = 0 if !(str_exog_age>=6 & str_exog_age<=9) & str_GPsexMatch!=.
gen str_exog_AgeSwapBins2 =  str_GPsexMatch if str_exog_age>=10 & str_exog_age<=12 
replace str_exog_AgeSwapBins2 = 0 if !(str_exog_age>=10 & str_exog_age<=12) & str_GPsexMatch!=.
gen str_exog_AgeSwapBins3 =  str_GPsexMatch if str_exog_age>=13 & str_exog_age!=.
replace str_exog_AgeSwapBins3 = 0 if !(str_exog_age>=13 & str_exog_age!=.) & str_GPsexMatch!=.
label var str_exog_AgeSwapBins1 "6-9"
label var str_exog_AgeSwapBins2 "10-12"
label var str_exog_AgeSwapBins3 "13+"
*Analysis
eststo clear
foreach out in $outc{
eststo: areg `out' ///
str_exog_AgeSwapBins1 str_exog_AgeSwapBins2 str_exog_AgeSwapBins3 ///
i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if sex==2 & exposure_str_exog_swap!=., cl(str_exogGPID) absorb(str_exogGPIDprev)
}
esttab using "Output\Table_A8_Female_AgeOfSwap.txt", replace ///
	keep(str_exog_AgeSwapBins1 str_exog_AgeSwapBins2 str_exog_AgeSwapBins3) ///
	label nonumber b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	note("Previous GP FE, municipality FE, cohort FE, and year FE")
}			

			

*Table A10: Balance test, boys
{
*Load and merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
*Restrict sample to be consistent w/main results
drop if STEM_HSdiploma==.
*Declare globals
gl char "birthorder siblings NORborn " 
gl char "$char mom_edu_yrs_15 mom_age_birth mom_married " 
gl char "$char mom_log_tot_inc_15 mom_NoLFP_15 ""
*Analysis
eststo clear
foreach out in $char{
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if sex==1 & exposure_str_exog_swap!=., cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_A10_Male_BalanceTeset.csv", replace ///
	keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	addnote("Previous GP FE; municipality FE; cohort FE; and year FE." ///
	"Sample limited to those with education data.")
}



*Table A11: Balance test without previous doctor fixed effects
{
*Load and merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
*Restrict sample to be consistent w/main results
drop if STEM_HSdiploma==.
*Declare globals
gl char "birthorder siblings NORborn " 
gl char "$char mom_edu_yrs_15 mom_age_birth mom_married " 
gl char "$char mom_log_tot_inc_15 mom_NoLFP_15 ""
*Analysis
eststo clear
foreach out in $char{
eststo: reg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if sex==2 & exposure_str_exog_swap!=., cl(str_exogGPID) 
estadd ysumm
}
esttab using "Output\Table_A11_Female_BalanceTeset_NoPrevGPFE.csv", replace ///
	keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	addnote("Previous GP FE; municipality FE; cohort FE; and year FE." ///
	"Sample limited to those with education data.")
}



*Table A12: Effect of same-gender GP on ... of girls, restricted sample
{
*Load and merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
merge 1:1 lopenr using "MomHealthOutcomes.dta"
*Globals of variables
gl out_core "Acad_track_Yr1 STEM_HSdiploma SciGPA SciGPA_HS "
gl char "birthorder siblings NORborn " 
gl char "$char mom_edu_yrs_15 mom_age_birth mom_married " 
gl char "$char mom_log_tot_inc_15 mom_NoLFP_15 "
*Determine restricted sample
drop if STEM_HSdiploma==. //same sample as balance test
areg str_GPsexMatch $char i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if sex==2 & exposure_str_exog_swap1!=., cl(str_exogGPID) absorb(str_exogGPIDprev)
gen AllBalance = 1 if (e(sample)==1) 
*Analysis
eststo clear
foreach out in $out_core{
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if sex==2 & exposure_str_exog_swap!=. & AllBalance==1, ///
cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_A12_RestrictSample.csv", replace keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	note("Previous GP FE, municipality FE, cohort FE, and year FE")
}			



*Table A13: Correlation between GP match and nongender GP characteristics
{
*Load and merge data
use "MainRHS.dta", clear
drop if lopenr==""
gen GPID=str_exogGPID
drop _merge
merge m:1 GPID using "GP_Chars.dta"
drop _merge
gen Age_GP = yr_str_exog_swap - YearBorn_GP

***Balance on GP chars
eststo clear
foreach out in Norway_GP Age_GP {					
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///	
	if sex==2 & exposure_str_exog_swap!=. & STEM_HSdiploma!=., ///
	cl(str_exogGPID) absorb(str_exogGPIDprev)
	estadd ysumm
}
esttab using "Output\Table_A13_GPGenderAndGPChar.csv", replace keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	addnote("Main model (previous GP FE; municipality FE; cohort FE; year FE)" )
}



*Table A14: Differences in outcomes by socioeconomic status
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
*Globals of variables
gl out_core "Acad_track_Yr1 STEM_HSdiploma SciGPA SciGPA_HS "
*SES variables
gen MomHighEdCollege = 1 if mom_edu_yrs>=16 & mom_edu_yrs<21.5
replace MomHighEdCollege = 0 if mom_edu_yrs<16
gen MomMarried = 1 if mom_married==1
replace MomMarried = 0 if mom_married==0
sum mom_log_tot_inc_15, det
gen MomHighInc75p = 1 if mom_log_tot_inc_15>=r(p75) & mom_log_tot_inc_15!=.
replace MomHighInc75p = 0 if mom_log_tot_inc_15<r(p75)
*Analysis
eststo clear
foreach out in $out_core{					
eststo: reg `out' MomHighEdCollege 
eststo: reg `out' MomHighInc75p 
eststo: reg `out' MomMarried 
}
esttab using "Output\Table_A14_DifferencesBySES.csv", replace ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	
}			



*Table A15: Instrumental variable estimation			
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
*Globals of variables
gl out_core "Acad_track_Yr1 STEM_HSdiploma SciGPA SciGPA_HS "
*Any GP gender match variable
gen YearsGPGenderMatch = 0
forvalues yr = 2015(-1)2002{
*Add 1 if under 15 and gender match
replace YearsGPGenderMatch = YearsGPGenderMatch + 1 ///
	if ((`yr' - Byr < 15) & (`yr' - Byr >= 0))  /// 
	& ((GPsex_`yr'==1 & sex==1) | (GPsex_`yr'==0 & sex==2))  
}
gen AnyGPMatch = str_GPsexMatch
replace AnyGPMatch = 1 if YearsGPGenderMatch>0 & YearsGPGenderMatch!=.
*Analysis
eststo clear
foreach out in $out_core{					
ivreg2 `out'  i.yr_str_exog_swap i.Byr i.str_exog_muni i.str_exogGPIDprev ///
	(AnyGPMatch=str_GPsexMatch) ///
	if sex==2 & exposure_str_exog_swap!=., cl(str_exogGPID) first
eststo
gl `out' = e(rkf)
}
esttab using "Output\Table_A14_IVEstimates.csv", replace ///
	keep(AnyGPMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	title("IV reg") ///
	addnote("IV is GP gender match;" ///
	"treatment is any years (binary) with same-gender GP." ///
	"Acad_track_Yr1 First Stage F: $Acad_track_Yr1" ///
	"STEM_HSdiploma First Stage F: $STEM_HSdiploma" ///
	"SciGPA First Stage F: $SciGPA" ///
	"SciGPA_HS  First Stage F: $SciGPA_HS ")
}			



*Table A16: Alternative balancing excercise
{
*Load and merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
*Restrict sample to be consistent w/main results
drop if STEM_HSdiploma==.
*Declare globals
gl char "birthorder siblings NORborn " 
gl char "$char mom_edu_yrs_15 mom_age_birth mom_married " 
gl char "$char mom_log_tot_inc_15 mom_NoLFP_15 ""
*Analysis
eststo clear
foreach out in $out_core{	
*Get predicted values				
reg `out' $charb if sex==2 & exposure_str_exog_swap==. & (Byr>=1988 & Byr<=1996)
predict `out'predict, xb
*Balance test using predicted values
eststo: areg `out'predict str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///	
	if sex==2 & exposure_str_exog_swap!=. & `out'!=., ///
	cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_A15_AltBalanceTest.csv", replace keep(str_GPsexMatch) ///
label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
note("Outcome is predicted measures based on demographics amoung non-swappers.")
}			



*Table A17: Correlation between gender of first GP and education of girls			
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
*Globals of variables
gl out_core "Acad_track_Yr1 STEM_HSdiploma SciGPA SciGPA_HS "
*First GP variables
forvalues yr = 2014(-1)2002{
gen GPfemale_FirstGP = (GPsex_`yr'==0) if  (GPsex_`yr' != .)
gen FirstGPID = GPID`yr' if (GPsex_`yr' != .)
gen FirstMuni = bokomm`yr' if (GPsex_`yr' != .)
gen FirstYr = `yr' if (GPsex_`yr' != .)
}
label var GPfemale_FirstGP "First GP is female"
label var GPmale_FirstGP "First GP is male"
label var FirstGPID "First GP ID"
*Analysis
eststo clear
foreach out in $out_core{
eststo: reg `out' GPfemale_FirstGP i.FirstYr i.Byr i.FirstMuni ///
	if sex==2 & STEM_HSdiploma!=. & exposure_str_exog_swap==., cl(FirstGPID) 
estadd ysumm
}
esttab using "Output\Table_A16_FirstGPGenderCorr.csv", replace ///
keep(GPfemale_FirstGP) ///
label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
note("municipality FE, cohort FE, and year FE")
}



*Table A18: Effect of same-gender GP on females, by father’s education
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
*Globals of variables
gl out_core "Acad_track_Yr1 STEM_HSdiploma SciGPA SciGPA_HS "
*Panel A: Father college or more
eststo clear
foreach out in $out_core{
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if (dad_edu_yrs>=16 & dad_edu_yrs<21.5) & sex==2 & exposure_str_exog_swap!=., ///
cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_A18_Female_DadCollOrMore.csv", replace ///
	keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	note("Previous GP FE, municipality FE, cohort FE, and year FE")
*Panel B: Father less than college
eststo clear
foreach out in $out_core{
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni ///
if (dad_edu_yrs<16) & sex==2 & exposure_str_exog_swap!=., ///
cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_A18_Female_DadLessColl.csv", replace ///
	keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	note("Previous GP FE, municipality FE, cohort FE, and year FE")	
}



*Table A19: Effect ... education of girls, controlling for current GP characteristics
{
*Merge data
use "MainRHS.dta", clear
drop if lopenr==""
merge 1:1 lopenr using "EdOutcomes.dta"
drop _merge
gen GPID=str_exogGPID
drop _merge
merge m:1 GPID using "GP_Chars.dta"
drop _merge
*Globals of variables
gl out_core "Acad_track_Yr1 STEM_HSdiploma SciGPA SciGPA_HS "
*Deal with missing  GP char
gen YearBorn_GP_miss=1 if YearBorn_GP==.
replace YearBorn_GP_miss=0 if YearBorn_GP!=.
replace YearBorn_GP=0 if YearBorn_GP==.
gen Norway_GP_miss=1 if Norway_GP==.
replace Norway_GP_miss=0 if Norway_GP!=.
replace Norway_GP=0 if Norway_GP==.
***Main analysis with current GP chars
eststo clear
foreach out in $out_core{					
eststo: areg `out' str_GPsexMatch i.yr_str_exog_swap i.Byr i.str_exog_muni /// 
	YearBorn_GP Norway_GP YearBorn_GP_miss Norway_GP_miss ///	
	if sex==2 & exposure_str_exog_swap!=., cl(str_exogGPID) absorb(str_exogGPIDprev)
estadd ysumm
}
esttab using "Output\Table_A19_MainAnalysisWithGPChar.csv", replace ///
	keep(str_GPsexMatch) ///
	label nonumber stats(ymean N) b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)	///
	addnote("Main model and current GP chars" )
}








